package com.parking.mgr.useraccount.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parking.mgr.common.exception.LoginAuthException;
import com.parking.mgr.common.util.IdUtils;
import com.parking.mgr.useraccount.enums.UserExceptionEnum;
import com.parking.mgr.useraccount.facede.UserServiceFacede;
import com.parking.mgr.useraccount.model.entity.UserAccountEntity;
import com.parking.mgr.useraccount.model.entity.UserInfoEntity;
import com.parking.mgr.useraccount.model.mapper.UserAccountMapper;
import com.parking.mgr.useraccount.model.param.UserAccountParam;
import com.parking.mgr.useraccount.service.UserAccountService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName UserAccountService
 * @Description 用户账号service
 * @Auther: zhijianpu
 * @Date: 2023/5/25
 */
@Log4j2
@Service
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccountEntity> implements UserAccountService {

    @Resource
    private UserAccountMapper userAccountMapper;

    @Resource
    private UserServiceFacede userServiceFacede;

    @Override
    public Long countByAccount(UserAccountParam userAccountParam) {
        String account = userAccountParam.getFAccount();
        LambdaQueryWrapper<UserAccountEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserAccountEntity::getFAccount, account);

        return this.count(queryWrapper);
    }

    @Override
    public void addUserAccount(String passportId, String account) {
        UserAccountEntity userAccountEntity = new UserAccountEntity();
        userAccountEntity.setFAccount(account);
        userAccountEntity.setFkUserPassportId(passportId);
        userAccountEntity.setId(IdUtils.getId());

        this.save(userAccountEntity);
    }

    @Override
    public void deleteAccountByPassportId(String passportId) {
        // 通过所属账号ID删除账号信息
        userAccountMapper.delete(new LambdaQueryWrapper<UserAccountEntity>()
                .eq(UserAccountEntity::getFkUserPassportId, passportId));
    }

    @Override
    public void saveOrUpdateUserAccount(UserAccountParam userAccountParam) {
        // 检查用户账号是否有重名
        checkParam(userAccountParam);
        log.info("保存或更新用户账号信息");
        String userInfoId = userAccountParam.getId();
        log.info("用户信息ID为: " + userInfoId);
        // 获取用户信息实体
        UserInfoEntity userInfoEntity = userServiceFacede.getUserInfoById(userInfoId);
        // 获取用户所属账号ID
        String passportId = userInfoEntity.getFkUserPassportId();
        // 查询用户是否有账号信息
        UserAccountEntity userAccountEntity = userAccountMapper.selectOne(new LambdaQueryWrapper<UserAccountEntity>()
                .eq(UserAccountEntity::getFkUserPassportId, passportId));

        if (ObjectUtil.isNull(userAccountEntity)) {
            log.info("保存用户账号信息");
            // 创建一个保存的实体类
            UserAccountEntity addUserAccountEntity = new UserAccountEntity();
            addUserAccountEntity.setFAccount(userAccountParam.getFAccount());
            addUserAccountEntity.setFkUserPassportId(passportId);
            // 保存
            this.save(addUserAccountEntity);
        } else {
            log.info("更新用户账号信息");
            userAccountEntity.setFAccount(userAccountParam.getFAccount());
            // 更新
            this.updateById(userAccountEntity);
        }
    }

    /**
     * 校验参数，检查是否存在相同的账号
     *
     * @param userAccountParam
     */
    private void checkParam(UserAccountParam userAccountParam) {

        Long countByAccount = this.countByAccount(userAccountParam);

        // 大于等于1则表示重复
        if (countByAccount >= 1) {
            throw new LoginAuthException(UserExceptionEnum.USER_ACCOUNT_REPEAT.getMessage());
        }
    }
}
